package com.heima.behavior.service.Impl;

import com.alibaba.fastjson.JSON;
import com.heima.behavior.service.ApBehaviorEntryService;
import com.heima.behavior.service.ApReadBehaviorService;
import com.heima.common.constants.article.HotArticleConstants;
import com.heima.common.constants.message.UpdateArticleMess;
import com.heima.common.exception.CustException;
import com.heima.model.behavior.dto.ReadBehaviorDTO;
import com.heima.model.behavior.pojos.ApBehaviorEntry;
import com.heima.model.behavior.pojos.ApReadBehavior;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.threadlocal.AppThreadLocalUtils;
import com.heima.model.user.pojos.ApUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
@Slf4j
public class ApReadBehaviorServiceImpl implements ApReadBehaviorService {
  @Autowired ApBehaviorEntryService apBehaviorEntryService;

  @Autowired MongoTemplate mongoTemplate;
  @Autowired RabbitTemplate rabbitTemplate;

  @Override
  public ResponseResult readBehavior(ReadBehaviorDTO dto) {
    // 检查参数
    ApUser user = AppThreadLocalUtils.getUser();

    ApBehaviorEntry apBehaviorEntry = null;
    if (user == null) {
      apBehaviorEntry =
          apBehaviorEntryService.findByUserIdOrEquipmentId(null, dto.getEquipmentId());
    } else {
      apBehaviorEntry =
          apBehaviorEntryService.findByUserIdOrEquipmentId(user.getId(), dto.getEquipmentId());
    }
    // 查询行为实体
    if (apBehaviorEntry == null) {
      CustException.cust(AppHttpCodeEnum.DATA_NOT_EXIST, "获取实体类失败");
    }
    //  查询指定文章阅读行为是否存在
    Query query =
        Query.query(
            Criteria.where("entryId")
                .is(apBehaviorEntry.getId())
                .and("articleId")
                .is(dto.getArticleId()));
    ApReadBehavior readBehavior = mongoTemplate.findOne(query, ApReadBehavior.class);

    // 不存在    新增阅读行为
    if (readBehavior == null) {
      ApReadBehavior apReadBehavior = new ApReadBehavior();
      apReadBehavior.setEntryId(String.valueOf(user.getId()));
      apReadBehavior.setArticleId(dto.getArticleId());
      apReadBehavior.setCount((short) 1);
      apReadBehavior.setCreatedTime(new Date());
      apReadBehavior.setUpdatedTime(new Date());
      mongoTemplate.save(apReadBehavior);

    } else {
      // 存在     更新阅读次数
      readBehavior.setCount((short) (readBehavior.getCount() + 1));
      mongoTemplate.save(readBehavior);
    }
//发送消息到Mq上
    UpdateArticleMess mess = new UpdateArticleMess();
    mess.setArticleId(dto.getArticleId());
    mess.setType(UpdateArticleMess.UpdateArticleType.VIEWS);
    mess.setAdd(1);
    String s = JSON.toJSONString(mess);
    rabbitTemplate.convertAndSend(HotArticleConstants.HOT_ARTICLE_SCORE_BEHAVIOR_QUEUE, s);
    log.info("阅读行为发送消息  {}", s);
    return ResponseResult.okResult();
  }
}
